package horse;

import java.awt.Point;
import java.util.ArrayList;

public class Horsewidth extends Horse implements iFindWay
{
	public Horsewidth(int size_x, int size_y)
	{
		super( size_x, size_y);
	}
	
	public ArrayList<Point> check( ArrayList<Point> points)
	{
		ArrayList<Point> result = new ArrayList<Point>();
		Area current, next;
		
		for (int i=0; i<points.size(); i++)
		{
			current = area.get(i+step);

			for (int j=0; j<8; j++)
			{
				if ( current.checkEnd( points.get(i).x+moves[j][0], points.get(i).y+moves[j][1] )) 
				{
					ways.add(current, step);
				}
				else
				{
					if (current.checkStep( points.get(i).x+moves[j][0], points.get(i).y+moves[j][1] ))
					{
						next = current.clone();
						next.setValue(points.get(i).x+moves[j][0], points.get(i).y+moves[j][1], step+1 );
						area.add( next );
						
						result.add(new Point( points.get(i).x+moves[j][0], points.get(i).y+moves[j][1] ));
					}
				}
			}
			step++;
		}
		if (points.isEmpty())
		{
			step++;
		}
		return result;
	}
	
	
	private void step( int x ,int y )
	{
		ArrayList<Point> points = new ArrayList<Point>();
		points.add(new Point( x, y ));
		step=0;
		
		while (step<MaxStep)
		{
			points = check(points);
			
		}
	}
	
	@Override
	public boolean find( int x ,int y, int x1, int y2)
	{
		Area init = area.get(0);
		init.setStartPoint(x, y);
		init.setEndPoint(x1, y2);
		area.set(0, init);
		
		step( x+2 , y+2 );
		
		if (ways.size()>0)
		{
			return true;
		}
		
		return false;
	}
	
}
